import os
import shutil

def delete_pycache_folders(root_dir):
    """
    递归删除指定目录下的所有 __pycache__ 文件夹。

    :param root_dir: 要搜索和删除 __pycache__ 文件夹的根目录
    """
    for dirpath, dirnames, filenames in os.walk(root_dir, topdown=False):
        for dirname in dirnames:
            if dirname == '__pycache__':
                pycache_path = os.path.join(dirpath, dirname)
                print(f"Deleting: {pycache_path}")
                shutil.rmtree(pycache_path)

if __name__ == "__main__":
    current_file_path = os.path.abspath(__file__)
    root_directory = os.path.dirname(current_file_path)
    print(f"当前文件所在目录: {root_directory}")
    if os.path.exists(root_directory) and os.path.isdir(root_directory):
        delete_pycache_folders(root_directory)
        print("所有 __pycache__ 文件夹已删除。")
    else:
        print("指定的路径不存在或不是一个有效的目录。")
